/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Apr 4, 2012  5:19:57 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.promis.sales
 * File Name : CustomerContractServices.java
 * TODO
 */
package jpa.service.promis.sales;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.common.CommonEntity;
import jpa.entitys.promis.sales.CustomerContract;
import jpa.entitys.promis.sales.CustomerContractPK;
import jpa.service.common.JPAService;

/**
 * @author chatree
 *
 */
public class CustomerContractServices extends JPAService {
	
	
	private static final Logger log = Logger.getLogger(CustomerContractServices.class);

	/**
	 * @param em
	 */
	public CustomerContractServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}
	
	

	public CustomerContract findById(CustomerContractPK id) throws Exception {
		
		return super.ConvertToYoureObject(super.find(id, "CustomerContract.findById"));				
	}
	
	public CustomerContract InsertedOrUpdated(CustomerContract vlaue) throws Exception {
		CustomerContract obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getLastUpdOn()))
			{					
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.inserted(obj);
			}else{				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public CustomerContract Updated(CustomerContract vlaue) throws Exception {
		CustomerContract obj = vlaue;
		try{
			
			if(vlaue.getId() != null)
			{
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}				
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	public List<CustomerContract> Inserted(List<CustomerContract> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(CustomerContract obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);
				}
			}		
			
			if(common_lst != null && common_lst.size() >= 1 && vlauelist != null && vlauelist.size() > 1)
			{
				if(common_lst.size() == vlauelist.size())
					super.inserted(common_lst);
			}
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<CustomerContract> Updated(List<CustomerContract> vlauelist) throws Exception {		
		
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();
			for(CustomerContract obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getLastUpdOn()))
				{					
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					common_lst.add(obj);					
				}
			}
			
			if(common_lst != null && common_lst.size() >= 1 && vlauelist != null && vlauelist.size() > 1)
			{
				if(common_lst.size() == vlauelist.size())
					super.updated(common_lst);
			}				
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}

}
